// pages/xdzglogin/xdzglogin.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  onLoad: function (options) {
    var _ = this;
    //  登录 
    wx.login({
      success(res) {
        if (res.code) {
          // 发起网络请求 传入code
          wx.request({
            url: 'https://zg99.offcn.com/index/wechat/getsesionkey?actid=50509&callback=?', //code换取sesionkey
            data: {
              code: res.code
            },
            success(res) {
              console.log('res ', res.data);
              if (res.data.status == 1) {
                console.log('获取成功 ' + res.data.session_key) //code成功换取到的sesionkey
                _.setData({
                  session_key: res.data.session_key
                })
              } else {
                console.log('获取失败 ' + res.data.msg)
              }
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
    console.log(options)
    _.setData({
      area: options.area,
      agent: options.agent,
    })
    if (options.page) {
      _.setData({
        page: options.page
      })
    }
  },
  getPhoneNumber(e) {
    var _ = this;
    var area = _.data.area;
    var agent = _.data.agent;
    var flag = _.data.flag;
    var key = _.data.session_key;
    var encryptedData = e.detail.encryptedData;
    var iv = e.detail.iv;
    if (flag) {
      console.log(flag)
      if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击了拒绝
        _.hideModal();
      } else { //允许授权执行跳转
        wx.request({
          url: "https://zg99.offcn.com/index/wechat/getphone?actid=50509&callback=?", //手机号解密
          data: {
            sessionKey: key, //登录获取到的sessionKey
            encryptedData: encryptedData,
            iv: iv
          },
          success(res) {
            console.log('res', res.data);
            if (res.data.status == 1) {
              var phone = res.data.phone; //获取得到手机号
              console.log("手机号=", phone)
              wx.setStorageSync('kxjphone', phone)
              wx.request({
                url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
                method: 'POST',
                data: {
                  activeId: 21,
                  mobile: phone,
                  areaCode: _.data.area,
                  agent: _.data.agent,
                  pageId: _.data.page
                },
                success: res => {

                }
              })
              wx.request({ //发送请求
                url: "https://zg99.offcn.com/index/choujiang/register/?actid=50509&callback=?",
                data: {
                  phone: phone,
                  isagree: true,
                  area: area,
                  agent: agent
                }, //接收参数
                success(res) { //返回
                  var jsonStr = res.data;
                  jsonStr = jsonStr.replace("(", "");
                  jsonStr = jsonStr.replace(")", "");
                  if (typeof jsonStr != 'object') {
                    jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                    var theJson = JSON.parse(jsonStr);
                  };
                  console.log(theJson)
                  wx.navigateTo({
                    url: '../index/index',
                  })
                }
              })
            } else {
              console.log("获取手机号失败 ", res.data.msg)
            }
          }
        })
      }

    } else {
      wx.showToast({
        title: '请先勾选隐私协议！',
        icon: 'none'
      })
    }

  },
  xz: function (e) {
    var _ = this;
    var flag = e.detail.value[0];
    if (flag == undefined) {
      flag = false
    } else {
      flag = true
    }
    _.setData({
      flag
    })
    console.log(_.data.flag)
  },
  xy: function (e) {
    var _ = this;
    wx.navigateTo({
      url: '../agreement/agreement',
    })
  },





  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})